;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.SapErrorList", [])
        .controller("SapErrorListCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };

            $scope.businessTypes = [
                {id: 'XGFR', name: "其它入库"},
                {id: 'XGFT', name: "发货通知"},
                {id: 'XGFC', name: "其它出库"},
                {id: 'XGLL', name: "领料单"},
                {id: 'XGQD', name: "区域调拨"},
                {id: 'XGDB', name: "代理商间调拨"},
                {id: 'XGCX', name: "冲销入库"},
                {id: 'XGTH', name: "退货入库"},
                {id: 'XGSC', name: "三包出库"},
                {id: 'XGCI', name: "采购入库"},
                {id: 'XGQR', name: "期初入库"},
                {id: 'XGPS', name: "配送指令入库"},
                {id: 'XGJY', name: "借用出库"}
            ];

            $scope.condition = {
                beginDate: ybWidget.FormatStartDate(+new Date()),
                orderCreateName:!!$scope.curUser.name ? $scope.curUser.name:""
            };
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1600,
                tableCol: [
                    {
                        type: "",
                        name: "备件系统单号",
                        dataField: "businessNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "SAP单据号",
                        dataField: "sapOrderNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单据类型",
                        dataField: "businessTypeShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件号",
                        dataField: "materialNo",
                        width: 170,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "行号",
                        dataField: "rowNo",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "错误信息",
                        dataField: "errorMessage",
                        width: 280,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "单据制单人",
                        dataField: "orderCreateName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createTimeShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        name: "采购组",
                        dataField: "purchaseGroup",
                        width: 100,
                        isShow: $scope.isDL || $scope.isZHL,
                        isFixed: false,
                    },
                    {
                        name: "采购凭证",
                        dataField: "voucherNo",
                        width: 120,
                        isShow: $scope.isDL || $scope.isZHL,
                        isFixed: false,
                    },
                    {
                        name: "采购凭证行号",
                        dataField: "voucherProNo",
                        width: 120,
                        isShow: $scope.isDL || $scope.isZHL,
                        isFixed: false,
                    }
                ]
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/SappostErrorRecord/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/SappostErrorRecord/exportAll")
            };

            var btnPermissionCode = {
                sapErrorExport: window.ActionCode.sapErrorExport
            };

            $scope.btnPermission = {
                sapErrorExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/SappostErrorRecord/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createTimeShow = $filter("YbDate")(item.createDate,"yyyy-MM-dd HH:mm:ss");
                        item.businessTypeShow = $filter("orderType")(item.businessType);
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };

            (function () {
                getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);